home *** CD-ROM | disk | FTP | other *** search
/ Network PC / Network PC.iso / amiga utilities / communication / internet / amitcp3.0b / src.lha / src / amitcp / net / if_loop.c next >
Encoding:
C/C++ Source or Header  |  1996-09-08  |  5.5 KB  |  228 lines

  1. RCS_ID_C="$Id: if_loop.c,v 1.7 1993/06/04 11:16:15 jraja Exp $";
  2. /* 
  3.  * Copyright (c) 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>,
  4.  *                    Helsinki University of Technology, Finland.
  5.  *                    All rights reserved.
  6.  *
  7.  * if_loop.c -- Local Loopback Device Functions
  8.  *
  9.  * Last modified: Fri Jun  4 00:37:54 1993 jraja
  10.  *
  11.  * HISTORY
  12.  * $Log: if_loop.c,v $
  13.  * Revision 1.7  1993/06/04  11:16:15  jraja
  14.  * Fixes for first public release.
  15.  *
  16.  * Revision 1.6  1993/05/16  21:09:43  ppessi
  17.  * RCS version changed.
  18.  *
  19.  * Revision 1.5  1993/04/11  22:21:18  jraja
  20.  * Removed unnecessary casts from function pointers, Changed ellipses to
  21.  * prototypes.
  22.  *
  23.  * Revision 1.4  93/04/05  17:45:56  17:45:56  jraja (Jarno Tapio Rajahalme)
  24.  * Changed spl storage variables to spl_t.
  25.  * Changed every .c file to use conf.h.
  26.  * 
  27.  * Revision 1.3  93/03/13  17:13:22  17:13:22  ppessi (Pekka Pessi)
  28.  * Fixed variable initialization problems.
  29.  * 
  30.  * Revision 1.2  93/03/02  18:49:09  18:49:09  too (Tomi Ollila)
  31.  * Changed %? to %l? on format strings
  32.  * 
  33.  * Revision 1.1  93/02/25  16:19:15  16:19:15  ppessi (Pekka Pessi)
  34.  * Initial revision
  35.  * 
  36.  */
  37.  
  38. /*
  39.  * Copyright (c) 1982, 1986 Regents of the University of California.
  40.  * All rights reserved.
  41.  *
  42.  * Redistribution and use in source and binary forms, with or without
  43.  * modification, are permitted provided that the following conditions
  44.  * are met:
  45.  * 1. Redistributions of source code must retain the above copyright
  46.  *    notice, this list of conditions and the following disclaimer.
  47.  * 2. Redistributions in binary form must reproduce the above copyright
  48.  *    notice, this list of conditions and the following disclaimer in the
  49.  *    documentation and/or other materials provided with the distribution.
  50.  * 3. All advertising materials mentioning features or use of this software
  51.  *    must display the following acknowledgement:
  52.  *    This product includes software developed by the University of
  53.  *    California, Berkeley and its contributors.
  54.  * 4. Neither the name of the University nor the names of its contributors
  55.  *    may be used to endorse or promote products derived from this software
  56.  *    without specific prior written permission.
  57.  *
  58.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  59.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  60.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  61.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  62.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  63.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  64.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  65.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  66.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  67.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  68.  * SUCH DAMAGE.
  69.  *
  70.  *    @(#)if_loop.c    7.13 (Berkeley) 4/26/91
  71.  */
  72.  
  73. /*
  74.  * Loopback interface driver for protocol testing and timing.
  75.  */
  76.  
  77. #include <conf.h>
  78.  
  79. #include <sys/param.h>
  80. #include <sys/systm.h>
  81. #include <sys/mbuf.h>
  82. #include <sys/socket.h>
  83. #include <sys/errno.h>
  84. #include <sys/ioctl.h>
  85. #include <sys/synch.h>
  86.  
  87. #include <net/if.h>
  88. #include <net/if_types.h>
  89. #include <net/netisr.h>
  90. #include <net/route.h>
  91.  
  92.  
  93. #if    INET
  94. #include <netinet/in.h>
  95. #include <netinet/in_systm.h>
  96. #include <netinet/in_var.h>
  97. #include <netinet/ip.h>
  98. #endif
  99.  
  100. #if NS
  101. #include <netns/ns.h>
  102. #include <netns/ns_if.h>
  103. #endif
  104.  
  105. #if ISO
  106. #include <netiso/iso.h>
  107. #include <netiso/iso_var.h>
  108. #endif
  109.  
  110. #include <net/if_loop_protos.h>
  111.  
  112. #define    LOMTU    (1024+512)
  113.  
  114. struct    ifnet loif = {0};
  115.  
  116. void
  117. loattach()
  118. {
  119.     register struct ifnet *ifp = &loif;
  120.  
  121.     ifp->if_name = "lo";
  122.     ifp->if_mtu = LOMTU;
  123.     ifp->if_flags = IFF_LOOPBACK;
  124.     ifp->if_ioctl = loioctl;
  125.     ifp->if_output = looutput;
  126.     ifp->if_type = IFT_LOOP;
  127.     ifp->if_hdrlen = 0;
  128.     ifp->if_addrlen = 0;
  129.     if_attach(ifp);
  130. }
  131.  
  132. int
  133. looutput(ifp, m, dst, rt)
  134.     struct ifnet *ifp;
  135.     register struct mbuf *m;
  136.     struct sockaddr *dst;
  137.     register struct rtentry *rt;
  138. {
  139.     int isr;
  140.     spl_t s;
  141.     register struct ifqueue *ifq = 0;
  142.  
  143.     if ((m->m_flags & M_PKTHDR) == 0)
  144.         panic("looutput no HDR");
  145.     m->m_pkthdr.rcvif = ifp;
  146.  
  147.     if (rt && rt->rt_flags & RTF_REJECT) {
  148.         m_freem(m);
  149.         return (rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
  150.     }
  151.     ifp->if_opackets++;
  152.     ifp->if_obytes += m->m_pkthdr.len;
  153.     switch (dst->sa_family) {
  154.  
  155. #if INET
  156.     case AF_INET:
  157.         ifq = &ipintrq;
  158.         isr = NETISR_IP;
  159.         break;
  160. #endif
  161. #if NS
  162.     case AF_NS:
  163.         ifq = &nsintrq;
  164.         isr = NETISR_NS;
  165.         break;
  166. #endif
  167. #if ISO
  168.     case AF_ISO:
  169.         ifq = &clnlintrq;
  170.         isr = NETISR_ISO;
  171.         break;
  172. #endif
  173.     default:
  174.         printf("lo%ld: can't handle af%ld\n", ifp->if_unit,
  175.             dst->sa_family);
  176.         m_freem(m);
  177.         return (EAFNOSUPPORT);
  178.     }
  179.     s = splimp();
  180.     if (IF_QFULL(ifq)) {
  181.         IF_DROP(ifq);
  182.         m_freem(m);
  183.         splx(s);
  184.         return (ENOBUFS);
  185.     }
  186.     IF_ENQUEUE(ifq, m);
  187.     schednetisr(isr);
  188.     ifp->if_ipackets++;
  189.     ifp->if_ibytes += m->m_pkthdr.len;
  190.     splx(s);
  191.     return (0);
  192. }
  193.  
  194. void lortrequest(int cmd, 
  195.          struct rtentry *rt, 
  196.          struct sockaddr *sa)
  197. {
  198.     if (rt)
  199.         rt->rt_rmx.rmx_mtu = LOMTU;
  200. }
  201.  
  202. /*
  203.  * Process an ioctl request.
  204.  */
  205. int
  206. loioctl(register struct ifnet *ifp, int cmd, caddr_t data)
  207. {
  208.     register struct ifaddr *ifa;
  209.     int error = 0;
  210.  
  211.     switch (cmd) {
  212.  
  213.     case SIOCSIFADDR:
  214.         ifp->if_flags |= IFF_UP;
  215.         ifa = (struct ifaddr *)data;
  216.         if (ifa != 0 && ifa->ifa_addr->sa_family == AF_ISO)
  217.             ifa->ifa_rtrequest = lortrequest;
  218.         /*
  219.          * Everything else is done at a higher level.
  220.          */
  221.         break;
  222.  
  223.     default:
  224.         error = EINVAL;
  225.     }
  226.     return (error);
  227. }
  228.